//
//  AlipayDetailViewController.m
//  XTXOneToOne
//
//  Created by Canny on 2018/11/7.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "AlipayDetailViewController.h"
#import "WKPopView.h"
#import "AlipayEditAccountViewController.h"
#import "UIImageView+WebCache.h"
@interface AlipayDetailViewController ()


@property (nonatomic,strong)UITextField * name_textField;
@property (nonatomic,strong)UITextField * zhifubao_textField;

@property (nonatomic,strong)UIScrollView * SC;

@property (nonatomic,strong)UITextField * pop_textField;
@property (nonatomic,strong)UIAlertAction *sureAction;
@property (nonatomic,strong)UIView * QR;

@end

@implementation AlipayDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setProperty];
    [self createUI];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    
}

-(void)setProperty{
    
    self.view.backgroundColor=RGB(255, 255, 255);
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    
    self.baseVcTitle = @"添加收款账户";
    
}

-(void)createUI{
    
    [self.rightBtn setTitle:@"更多" forState:UIControlStateNormal];
    
    _SC=[[UIScrollView alloc]initWithFrame:CGRectMake(0, kStatusBarAndNavigationBarHeight, G_SCREENWIDTH, G_SCREENHEIGHT-kStatusBarAndNavigationBarHeight)];
    [self.view addSubview:_SC];
    CGFloat H = G_SCREENHEIGHT;
    //    if (G_SCREENHEIGHT<(20+6*90+400)) {
    //        H =20+6*90+400;
    //    }
    
    _SC.contentSize = CGSizeMake(G_SCREENWIDTH, H );
    NSString * str=@"";
    if ([_dataSource.symbol containsString:@"支付宝"]) {
        str = @"支付宝";
    }else{
        str = @"微信";
    }
    
    _name_textField = [[UITextField alloc]init];
    [self setText:@"姓名" andTF:_name_textField andIndex:0 andPlaceholder:@"请输入姓名" andKeyboardType:UIKeyboardTypeDefault];
    _zhifubao_textField = [[UITextField alloc]init];
    [self setText:[NSString stringWithFormat:@"%@账户",str] andTF:_zhifubao_textField andIndex:1 andPlaceholder:@"请输入账户" andKeyboardType:UIKeyboardTypeDefault];

    UILabel * lab = [[UILabel alloc]init];
    [self.SC addSubview:lab];
    [self setLab:lab withColor:RGB(30, 63, 88) andFont:12 andText:@"收款码" andTextAlignment:NSTextAlignmentLeft];
    lab.frame=CGRectMake(15, 20 + 2*90, 200, 30);
    
    
    UIButton *  btn1 = [[UIButton alloc]init];
    [self.SC addSubview:btn1];
    btn1.imageView.size=CGSizeMake(22, 21) ;
    [btn1 setTitleColor:ThemeColor forState:UIControlStateNormal];
//    [btn1 setTitle:@"" forState:UIControlStateNormal];
//    [btn1 setImage:[UIImage imageNamed:@"二维码icon"] forState:UIControlStateNormal];
    btn1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//    btn1.titleLabel.font = [UIFont systemFontOfSize: 15.0];
    [btn1 addTarget:self action:@selector(openQRcode) forControlEvents:UIControlEventTouchUpInside];
    btn1.backgroundColor=MC_TEST_COLOR;
    btn1.frame=CGRectMake(G_SCREENWIDTH-15-80, 20 + 2*90, 80, 30);

    
    UIImageView * imgV = [[UIImageView alloc]init];
    [btn1 addSubview:imgV];
    imgV.image = [UIImage imageNamed:@"二维码icon"];
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(btn1.mas_centerY).offset(0);
        make.right.equalTo(btn1.mas_right).offset(0);
        make.width.mas_equalTo(22);
        make.height.mas_equalTo(21);
    }];

    NSDictionary * account = _dataSource.account;

    _name_textField.text = [NSString stringWithFormat:@"%@",account[@"name"]];
    _zhifubao_textField.text = [NSString stringWithFormat:@"%@",account[@"account"]];

    
    
}

#pragma mark-查看二维码
-(void)openQRcode{
    
    UIView * QR = [[UIView alloc]init];
    [self.view addSubview:QR];
    QR.backgroundColor=[UIColor clearColor];
    [QR mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.bottom.equalTo(self.view);
    }];
    _QR=QR;
    
    UIButton *QRbtn = [[UIButton alloc]init];
    [QR addSubview:QRbtn];
    [QRbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.bottom.equalTo(QR);
    }];
    QRbtn.backgroundColor=[UIColor blackColor];
    QRbtn.alpha=0.5;
    
    UIImageView * imgV = [[UIImageView alloc]init];
    [QR addSubview:imgV];
    [imgV setContentMode:UIViewContentModeScaleAspectFill];
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(QR.mas_centerY);
        make.centerX.equalTo(QR.mas_centerX);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(200);
    }];
    
    NSDictionary * account = _dataSource.account;

    NSString * URL = [NSString stringWithFormat:@"%@",account[@"qrcode_url"]];
    [imgV sd_setImageWithURL:[NSURL URLWithString:URL]];

    [QRbtn addTarget:self action:@selector(disMissQRcode) forControlEvents:UIControlEventTouchUpInside];
  
    
}

-(void)disMissQRcode{
    WS(weakSelf);
    [UIView animateWithDuration:0.3 animations:^{
        [weakSelf.QR removeFromSuperview];
    }];

}

-(void)setText:(NSString *)text andTF:(UITextField *)textField andIndex:(NSInteger)index andPlaceholder:(NSString *)placeholder andKeyboardType:(UIKeyboardType)Type{
    CGFloat T = 20 + index*90;
    
    UILabel * lab = [[UILabel alloc]init];
    [self.SC addSubview:lab];
    [self setLab:lab withColor:RGB(30, 63, 88) andFont:12 andText:text andTextAlignment:NSTextAlignmentLeft];
    lab.frame=CGRectMake(15, T, 200, 30);
    
    
    [self.SC addSubview:textField];
    [self setTextField:textField WithPlaceholder:placeholder and:Type];
    textField.frame=CGRectMake(15, T+20, G_SCREENWIDTH-30, 40);
    
    UIView * line = [[UIView alloc]init];
    line.backgroundColor=RGB(231, 235, 238);
    [self.SC addSubview:line];
    line.frame=CGRectMake(15, T+20+39, G_SCREENWIDTH-30, 1);
    
}

-(void)setLab:(UILabel *)lab withColor:(UIColor *)color andFont:(CGFloat)font andText:(NSString *)text andTextAlignment:(NSTextAlignment)textAlignment{
    lab.text=text;
    lab.textColor=color;
    lab.font=[UIFont systemFontOfSize:font];
    lab.textAlignment=textAlignment;
}

-(void)setTextField:(UITextField*)textField WithPlaceholder:(NSString *)placeholder and:(UIKeyboardType)type  {
    
    
    textField.placeholder=placeholder;
    textField.borderStyle = UITextBorderStyleNone;
    textField.userInteractionEnabled = NO;
    //    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    //    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.backgroundColor=[UIColor clearColor];
    textField.font = [UIFont systemFontOfSize:15];
    textField.textColor = RGB(32, 64, 89);
    textField.textAlignment = NSTextAlignmentLeft;
    //    textField.returnKeyType = UIReturnKeyDefault;
    textField.keyboardType = type;
    [textField setValue:RGB(190, 190, 190) forKeyPath:@"_placeholderLabel.textColor"];
    [textField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    
}


-(void)textFieldDidChange:(UITextField *)textField{
    textField.text= [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    
}


#pragma mark-操作
-(void)rightAction{
    
    WS(weakSelf);
    
    WKPopView * popView=[WKPopView InitPopViewWithTitle:@"编辑" sureTitle:@"解除绑定" andCancelTitle:@"取消"];
    [popView show];
    popView.block = ^(NSInteger type) {
        //编辑
        if (type==1) {
            
            AlipayEditAccountViewController * VC = [[AlipayEditAccountViewController alloc]init];
            VC.dataSource = weakSelf.dataSource;
            [weakSelf.navigationController pushViewController:VC animated:YES];
            
            //解除绑定
        }else if (type==2){
            
            [weakSelf popCheckMoneyPassword];
            
        }
    };
    
    
}



-(void)popCheckMoneyPassword{
    
    WS(weakSelf);
    //创建UIAlertController 设置标题，信息，样式
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"解除绑定" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    
    //添加UITextField
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入资金密码";
        textField.keyboardType = UIKeyboardTypeDefault;
        [textField addTarget:weakSelf action:@selector(textFieldChane:) forControlEvents:UIControlEventEditingChanged];
        textField.secureTextEntry=YES;
        weakSelf.pop_textField=textField;
    }];
    
    //创建UIAlertAction对象，设置标题并添加到UIAlertController上
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:nil];
    [alertController addAction:cancelAction];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [weakSelf unbundle];
        
    }];
    sureAction.enabled=NO;
    _sureAction=sureAction;
    //设置UIAlertAction对象是否可用
    [alertController addAction:sureAction];
    
    //展现UIAlertController
    [self presentViewController:alertController animated:YES completion:nil];
}


/*
删除收款信息

1、接口地址

请求方式：post
请求接口：otcpayment/delpayment/
是否登录：是
2、接口请求参数（支付宝/微信支付）

参数名    参数类型    含义    备注
payment_id    整型    收款方式id
asset_password    字符串    资产密码
*/
#pragma mark-解绑
-(void)unbundle{
    
    
    NSDictionary * Dic =@{
                          @"payment_id":_dataSource.ID,
                          @"asset_password":_pop_textField.text,
                          @"name":_name_textField.text,
                          @"access_token":[LoginModel GetValue:DL_token]
                          };
    
    NSString * URL = [NSString stringWithFormat:@"%@otcpayment/delpayment/",DEFAULTSEVERBASEURL];
    
    WS(weakSelf);
    
    [HUD showProgress:@"" inView:self.bgView];
    
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        [HUD dismissInView:weakSelf.bgView];
        NSDictionary * dic = responseObject;
        
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
        if ([code isEqualToString:@"0"]) {
            
            [HUD showSuccessMessage:msg];
            
            //            [self performSelector:@selector(delayMent) withObject:nil afterDelay:2.0];
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
            
        }else{
            [HUD showErrorMessage:msg];
        }
        
    } FailureBlock:^(id  _Nonnull error) {
        
        [HUD dismissInView:weakSelf.bgView];
        
    }];
    
}
-(void)textFieldChane:(UITextField *)tf{
    if (tf.text.length>0) {
        _sureAction.enabled=YES;
    }else{
        _sureAction.enabled=NO;
    }
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

